<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: lh
 * Time: 2020/07/13
 * Desc: 积分相关功能：会员俱乐部，我的积分明细，积分抽奖，
 */
class Controller_Pc_Api_V2_Integral_Integral extends Controller_Pc_Api_V2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Integral_Integral
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Integral_Integral
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('integral_integral', $this->request_params);

        // 服务
        $this->service = new Service_Api_Integral_Integral();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 获取用户当前积分
    public function action_get_integral()
    {
        try {
            $this->validation->get_integral()->check();

            $this->result['data'] = $this->service->get_integral($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取会员积分记录
    public function action_get_user_credit_list()
    {
        try {
            $this->validation->get_user_credit_list()->check();

            $this->result['data'] = $this->service->get_user_credit_list($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * 产品绑定的积分策略（抵扣，赠送）
     */
    public function action_get_integral_strategy()
    {
        try {
            $this->validation->get_integral_strategy()->check();

            $this->result['data'] = $this->service->get_integral_strategy($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取会员签到状态
    public function action_get_member_sign_status()
    {
        try {
            $this->validation->get_member_sign_status()->check();

            $this->result['data'] = $this->service->get_member_sign_status($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 会员签到
    public function action_sign_in()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->sign_in()->check();

            if (!St_Functions::is_normal_app_install('member_sign')) {
                throw new Exception('应用未安装');
            }

            $this->result = $this->service->sign_in($this->mid);
            $this->result['status'] = true;

            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());

            $db->rollback();
        }
    }

    // 用户积分及等级
    public function action_integral_rank()
    {
        try {
            $this->validation->integral_rank()->check();

            $this->result['data'] = $this->service->integral_rank($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 积分任务
    public function action_get_grade_strategy()
    {
        //积分任务类型 1- 首页三个任务，2-新手任务，3-每日任务
        try {
            $this->validation->get_grade_strategy()->check();

            $this->result['data'] = $this->service->get_grade_strategy($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }
}